<template>
  <div class="app-container">
    <el-card class="box-card" shadow="always" v-show="showSearch">
    <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="68px">
      <el-form-item label="" prop="deviceName">
        <el-input
          v-model="queryParams.deviceName"
          placeholder="设备名称"
          clearable
          @keyup.enter="handleQuery"
          style="width:120px"
        />
      </el-form-item>
      <el-form-item label="" prop="deviceCode">
        <el-input
            v-model="queryParams.deviceCode"
            placeholder="管理编号"
            clearable
            @keyup.enter="handleQuery"
            style="width:120px"
        />
      </el-form-item>
      <el-form-item label="" prop="deviceProcode">
        <el-input
          v-model="queryParams.deviceProcode"
          placeholder="出厂编号"
          clearable
          @keyup.enter="handleQuery"
          style="width:120px"
        />
      </el-form-item>
      <el-form-item label="" prop="model">
        <el-input
          v-model="queryParams.model"
          placeholder="型号规格"
          clearable
          @keyup.enter="handleQuery"
          style="width:120px"
        />
      </el-form-item>
      <el-form-item label="" prop="deviceState">
        <el-select v-model="queryParams.deviceState" placeholder="状态" clearable style="width:120px">
          <el-option
            v-for="dict in lab_base_device_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    </el-card>

    <el-card class="box-card" shadow="always">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['labBase:device:add']"
        >新增</el-button>
      </el-col>
      <!--<el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['labBase:device:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['labBase:device:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['labBase:device:export']"
        >导出</el-button>
      </el-col>-->
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="deviceList" @selection-change="handleSelectionChange">
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200" fixed>
        <template #default="scope">
          <el-button
              type="text"
              icon="Edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['labBase:device:edit']"
          >修改</el-button>
          <el-button
              type="text"
              icon="Delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['labBase:device:remove']"
          >删除</el-button>
          <el-button
              type="text"
              icon="ZoomIn"
              @click="handlePreview(scope.row)"
              v-hasPermi="['labBase:device:query']"
          >详细</el-button>
        </template>
      </el-table-column>
      <el-table-column label="设备状态" align="center" prop="deviceState" width="80" fixed>
        <template #default="scope">
          <dict-tag :options="lab_base_device_state" :value="scope.row.deviceState"/>
        </template>
      </el-table-column>
      <el-table-column label="管理编号" align="center" prop="deviceCode" width="120"  show-overflow-tooltip fixed />
      <el-table-column label="设备名称" align="center" prop="deviceName"  width="280"  show-overflow-tooltip fixed/>
<!--      <el-table-column label="设备类别" align="center" prop="deviceType" />-->
      <el-table-column label="型号规格" align="center" prop="model" show-overflow-tooltip />
      <el-table-column label="出厂编号" align="center" prop="deviceProcode"  show-overflow-tooltip />
      <el-table-column label="生产厂家" align="center" prop="factoryName"  show-overflow-tooltip />
<!--      <el-table-column label="设备类别" align="center" prop="deviceType"  show-overflow-tooltip />-->
      <el-table-column label="检定周期" align="center" prop="deviceCycle"  show-overflow-tooltip />
      <el-table-column label="核查时间" align="center" prop="devicePeriodDate"  show-overflow-tooltip />
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    </el-card>

    <!-- 详细 -->
    <el-dialog title="详细信息" v-model="openDetails" width="800px" draggable append-to-body class="details">
      <info :id="deviceId"></info>
    </el-dialog>

  </div>
</template>

<script setup name="Devices">
import { listDevice, getDevice, delDevice, addDevice, updateDevice } from "@/api/labBase/device";
import { listDevicetype} from "@/api/labBase/devicetype";
import fileManager from "@/views/pub/fileManager";
import info from "@/views/labbase/device/details";
import {selectUser} from "@/api/system/tools"
import { deptTreeSelect } from "@/api/system/user";
import devicefun from "@/views/labbase/devicefun/index";



const { proxy } = getCurrentInstance();
const { lab_base_device_state } = proxy.useDict('lab_base_device_state');
let activeName = ref("tab1");
const deviceList = ref([]);
const open = ref(false);
const openDetails = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const deviceId = ref("");
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const typeCode = "DEVICE";
const deviceTypeOptions = ref(undefined);
const deptOptions = ref(undefined);
const saveUserOptions = ref(undefined);
let disabledtab = ref(true);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    deviceType: null,
    deviceCode: null,
    deviceName: null,
    deviceProcode: null,
    model: null,
    deviceState: null,
  },
  rules: {
    deviceType:{required:true,message:'请选择设备类别',trigger:'green'},
    deviceCode:{required:true,message:'请输入管理编号',trigger:'green'},
    deviceName: [
      { required: true, message: '请输入设备名称', trigger: 'blur' }
    ],
    model:{required:true,message:'请输入型号规格',trigger:'green'},
    deviceState:{required:true,message:'请选择设备状态',trigger:'green'}
  }
});

const { queryParams, form, rules } = toRefs(data);



/** 查询设备管理列表 */
function getList() {
  loading.value = true;
  listDevice(queryParams.value).then(response => {
    deviceList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    deviceId: null,
    deviceUuid: null,
    deviceType: null,
    deviceCode: null,
    deviceName: null,
    deviceEname: null,
    production: null,
    deviceProcode: null,
    model: null,
    attachment: null,
    deviceData: null,
    deviceDept: null,
    deviceDeptName: null,
    saveUser: null,
    saveUserName: null,
    useDept: null,
    useDeptName: null,
    devicePlace: null,
    buyDate: null,
    checkUser: null,
    checkDate: null,
    deviceState: null,
    deviceCycle: null,
    deviceScope: null,
    standardCert: null,
    standardEn: null,
    standard: null,
    devicePeriod: null,
    devicePeriodDate: null,
    stability: null,
    traceConfirm: null,
    arrowError: null,
    adminLevel: null,
    tools: null,
    deviceCount: null,
    traceType: null,
    traceParam: null,
    deviceUse: null,
    updateTime: null,
    updateBy: null,
    createTime: null,
    createBy: null,
    remark: null,
    invalidReason: null,
    supplierName: null,
    supplierPerson: null,
    supplierTel: null,
    factoryName: null,
    factoryPerson: null,
    factoryTel: null,
    factoryRemark: null,
  };
  proxy.resetForm("deviceRef");
  activeName.value = 'tab1'
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.deviceId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  disabledtab.value = true;
  title.value = "添加设备管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _deviceId = row.deviceId || ids.value
  getDevice(_deviceId).then(response => {
    form.value = response.data;
    form.value.deviceDept = form.value.deviceDept === "" ? null : parseInt(form.value.deviceDept);
    form.value.useDept = form.value.useDept === "" ? null : parseInt(form.value.useDept);
    form.value.deviceType = form.value.deviceType === "" ? null : parseInt(form.value.deviceType);
    open.value = true;
    disabledtab.value = false;
    title.value = "修改设备管理";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["deviceRef"].validate(valid => {
    if (valid) {
      if (form.value.deviceId != null) {
        updateDevice(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          //open.value = false;
          getList();
        });
      } else {
        addDevice(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _deviceIds = row.deviceId || ids.value;
  proxy.$modal.confirm('是否确认删除？').then(function() {
    return delDevice(_deviceIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('labBase/device/export', {
    ...queryParams.value
  }, `device_${new Date().getTime()}.xlsx`)
}


/*查看详细*/
function handlePreview(row) {
  openDetails.value = true;
  deviceId.value = row.deviceId;
}


function saveUserList(query){
  saveUserOptions.value = [];
  let param = {'userName':query}
  selectUser(param).then(response => {
    saveUserOptions.value = response;
    loading.value = false;
  });
}
function saveUserChange(e) {
  form.value.saveUser = e.value;
  form.value.saveUserName = e.label;
}
/** 查询部门下拉树结构 */
function getDeptTree() {
  deptTreeSelect().then(response => {
    deptOptions.value = response.data;
  });
};
/** 查询设备分类下拉树结构 */
function getDeviceType() {
  listDevicetype().then(response => {
    deviceTypeOptions.value = proxy.handleTree(response.data, "deviceTypeId", "parentId");
  });
};


const Options = [
  {
    value: 0,
    label: '否',
  },
  {
    value: 1,
    label: '是',
  }
]


getDeptTree();
getDeviceType()
getList();

</script>
